package zabbix

import (
	"encoding/json"
	"errors"
)

func MapCreateData() (string,map[string]interface{}) {
	method := "map.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Trigger map",
        "width": 600,
        "height": 600,
        "selements": [
            {
                "elements": [
                    {"triggerid": "12345"},
                    {"triggerid": "67890"}
                ],
                "elementtype": 2,
                "iconid_off": "2"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MapDeleteData() (string,map[string]interface{}) {
	method := "map.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "12",
        "34"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MapUpdateData() (string,map[string]interface{}) {
	method := "map.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "sysmapid": "8",
        "width": 1200,
        "height": 1200
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MapGetData() (string,map[string]interface{}) {
	method := "map.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "selectSelements": "extend",
        "selectLinks": "extend",
        "selectUsers": "extend",
        "selectUserGroups": "extend",
        "selectShapes": "extend",
        "selectLines": "extend",
        "sysmapids": "3"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func MapCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MapCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MapDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MapDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MapUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MapUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func MapGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := MapGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



